/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package hextd.tower;

import com.twolattes.json.Entity;
import com.twolattes.json.Value;
import java.util.Map;
import javax.swing.JPanel;

@Entity
public class JsonTowerOptions {

	public static enum optionKey {

		maxlevel("Maximum level", "9"),
		maxcount("Limit the muber of towers of this type", "10");
		public final String description;
		public final String defaultValue;

		private optionKey(String description, String defaultValue) {
			this.description = description;
			this.defaultValue = defaultValue;
		}
	}
	@Value
	public TowerFactory.type type;
	@Value(optional = true)
	public Map<String, String> options;
	//
	private JsonTowerOptionsPanel settingsPanel;
	private int maxCount = -1;
	private int maxLevel = -1;

	public int getMaxCount() {
		if (maxCount == -1) {
			if (options.containsKey(optionKey.maxcount.name())) {
				maxCount = Integer.parseInt(options.get(optionKey.maxcount.name()));
				if (maxCount < 0) {
					maxCount = 0;
				}
			} else {
				maxCount = Integer.MAX_VALUE;
			}
		}
		return maxCount;
	}

	public int getMaxLevel() {
		if (maxLevel == -1) {
			if (options.containsKey(optionKey.maxlevel.name())) {
				maxLevel = Integer.parseInt(options.get(optionKey.maxlevel.name()));
				if (maxLevel < 0) {
					maxLevel = 0;
				}
			} else {
				maxLevel = 9;
			}
		}
		return maxLevel;
	}

	@Override
	public String toString() {
		return type.toString();
	}

	public JPanel getSettingsPanel() {
		if (settingsPanel == null) {
			settingsPanel = new JsonTowerOptionsPanel(this);
		}
		return settingsPanel;
	}
}
